{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-02-25 16:43:47.050908\t开始加载历史数据\n",
      "2021-02-25 16:43:47.050908\t加载进度：# [0%]\n",
      "2021-02-25 16:43:47.259348\t加载进度：# [10%]\n",
      "2021-02-25 16:43:47.430888\t加载进度：## [19%]\n",
      "2021-02-25 16:43:47.704149\t加载进度：### [29%]\n",
      "2021-02-25 16:43:47.987408\t加载进度：#### [38%]\n",
      "2021-02-25 16:43:48.236704\t加载进度：##### [48%]\n",
      "2021-02-25 16:43:48.488060\t加载进度：###### [57%]\n",
      "2021-02-25 16:43:48.701492\t加载进度：####### [67%]\n",
      "2021-02-25 16:43:48.951821\t加载进度：######## [76%]\n",
      "2021-02-25 16:43:49.117373\t加载进度：######### [86%]\n",
      "2021-02-25 16:43:49.451473\t加载进度：########## [96%]\n",
      "2021-02-25 16:43:49.591086\t历史数据加载完成，数据量：23760\n",
      "price: 98.90060924848197\n",
      "price: 98.90642975288874\n",
      "price: 98.91074983759609\n",
      "price: 98.93976407222867\n",
      "price: 98.97263699569169\n",
      "price: 99.01064968220757\n",
      "price: 99.07024690184235\n",
      "price: 99.10425609367293\n",
      "price: 99.15067533564489\n",
      "price: 99.24252862654602\n",
      "price: 99.30361984339989\n",
      "price: 99.34355861976273\n",
      "price: 99.39434347734411\n",
      "price: 99.42193196885151\n",
      "price: 99.44022439352004\n",
      "price: 99.43850463805828\n",
      "price: 99.40996535589355\n",
      "price: 99.38267101898332\n",
      "price: 100.53305766472639\n",
      "price: 101.05202454712722\n",
      "price: 101.41677823306746\n",
      "price: 101.71495650163605\n",
      "price: 101.95892560556665\n",
      "price: 102.14587882874557\n",
      "price: 102.31689255025537\n",
      "price: 102.4356180437112\n",
      "price: 102.512838593359\n",
      "price: 101.623243028731\n",
      "price: 100.65279029304145\n",
      "price: 100.62470907320277\n",
      "price: 100.6158019536604\n",
      "price: 100.61335971925268\n",
      "price: 100.60483206440537\n",
      "price: 100.59421946246844\n",
      "price: 100.53479124770351\n",
      "2021-02-25 16:43:49.630022\t策略初始化完成\n",
      "2021-02-25 16:43:49.631027\t开始回放历史数据\n",
      "price: 100.54157202207459\n",
      "price: 100.55584621185469\n",
      "price: 100.58960283398513\n",
      "price: 100.6039379573005\n",
      "price: 100.6167310842747\n",
      "price: 100.64307365988343\n",
      "price: 100.66774512899141\n",
      "price: 100.68366038864711\n",
      "price: 100.69816973494173\n",
      "price: 100.70361900234312\n",
      "price: 100.70768311341632\n",
      "price: 100.69524444738154\n",
      "price: 100.57607045124553\n",
      "price: 100.5760704510286\n",
      "price: 100.56646023302658\n",
      "price: 100.56984425190043\n",
      "price: 100.86984696184017\n",
      "2021-02-25 16:43:49.691845\t回放进度：= [0%]\n",
      "price: 100.18892723859116\n",
      "price: 100.2041845138304\n",
      "price: 100.2112506373518\n",
      "price: 100.24948847013044\n",
      "price: 100.25603715656487\n",
      "price: 100.25483018897425\n",
      "price: 100.2210254114404\n",
      "price: 100.26088059944843\n",
      "price: 100.2731195671851\n",
      "price: 100.31763433832276\n",
      "price: 100.3563842918517\n",
      "price: 100.38648439557717\n",
      "price: 100.42431035668955\n",
      "price: 100.46538354119828\n",
      "price: 100.51154245241543\n",
      "price: 100.55340559908831\n",
      "price: 100.58909650513749\n",
      "price: 100.6148649637245\n",
      "price: 100.63699827938761\n",
      "price: 100.65519332031444\n",
      "price: 100.67279294933884\n",
      "price: 100.66328359026942\n",
      "price: 100.34054174046373\n",
      "2021-02-25 16:43:49.744704\t回放进度：== [10%]\n",
      "price: 101.09552359603342\n",
      "2021-02-25 16:43:49.789583\t回放进度：=== [20%]\n",
      "price: 102.20427112285134\n",
      "price: 102.15904812225723\n",
      "price: 102.11277821228973\n",
      "price: 101.80769264177401\n",
      "price: 101.62865312209286\n",
      "price: 101.52532036333544\n",
      "price: 101.53126709355497\n",
      "price: 101.55579303733191\n",
      "price: 101.55713709484573\n",
      "price: 101.56940888591443\n",
      "price: 101.58037045608309\n",
      "price: 101.60224086966035\n",
      "price: 101.59840989980624\n",
      "price: 101.62671418470318\n",
      "price: 101.65543180928618\n",
      "price: 101.6844260712254\n",
      "price: 101.04060824108028\n",
      "2021-02-25 16:43:49.837456\t回放进度：==== [30%]\n",
      "price: 101.28007271060923\n",
      "price: 101.23779547494996\n",
      "price: 101.21413863695038\n",
      "price: 101.20200922367145\n",
      "price: 101.19522370686984\n",
      "price: 101.19206101179749\n",
      "price: 101.16171414634161\n",
      "price: 101.15709627641729\n",
      "price: 101.16376421336439\n",
      "price: 101.20534294488608\n",
      "price: 101.21834430069649\n",
      "price: 101.243866903827\n",
      "price: 101.25514621234343\n",
      "price: 101.24161272287799\n",
      "price: 101.686529806477\n",
      "price: 101.68841496016307\n",
      "price: 101.7614173541721\n",
      "price: 101.79600705890535\n",
      "price: 101.79902737881051\n",
      "price: 101.79139413578287\n",
      "price: 101.77518228599232\n",
      "price: 101.78887270658147\n",
      "price: 101.80412583467455\n",
      "price: 101.82134147165395\n",
      "price: 101.84777730886388\n",
      "price: 101.87995781864215\n",
      "price: 101.72819988833383\n",
      "price: 101.7162097696604\n",
      "price: 101.7162097696604\n",
      "price: 101.71705605692287\n",
      "price: 101.73719619548449\n",
      "price: 101.74499739599938\n",
      "price: 101.75655797391535\n",
      "price: 101.75587336912336\n",
      "price: 101.75759390954858\n",
      "price: 101.75301282579177\n",
      "price: 101.78226236531621\n",
      "2021-02-25 16:43:49.896291\t回放进度：===== [40%]\n",
      "price: 102.17378806094271\n",
      "price: 102.12966246063012\n",
      "price: 102.08984961534472\n",
      "price: 102.21949393569658\n",
      "price: 102.75499319257793\n",
      "price: 102.75367022692095\n",
      "price: 102.72062204453769\n",
      "price: 102.59582284197927\n",
      "price: 102.56813680703226\n",
      "price: 102.5553059872436\n",
      "price: 102.55271333951704\n",
      "price: 102.5618099308164\n",
      "price: 102.5604988171575\n",
      "price: 102.51933994354178\n",
      "price: 102.40564818285388\n",
      "price: 102.40348604103382\n",
      "2021-02-25 16:43:49.945179\t回放进度：====== [50%]\n",
      "price: 102.41177327766758\n",
      "price: 102.41761584365771\n",
      "price: 102.42160013832495\n",
      "price: 102.41786459348363\n",
      "price: 102.40488664415462\n",
      "price: 102.3796214563005\n",
      "price: 102.39035382024227\n",
      "price: 102.4160171228044\n",
      "price: 102.4377947240484\n",
      "price: 102.45508228916205\n",
      "price: 102.4617654318119\n",
      "price: 102.49446096052768\n",
      "price: 102.51665269385838\n",
      "price: 102.53267426349183\n",
      "price: 102.51608981568424\n",
      "price: 102.48072542416628\n",
      "price: 102.46642366097025\n",
      "price: 102.48307171842735\n",
      "price: 102.49719026029223\n",
      "price: 102.4685050518923\n",
      "price: 102.44921341800034\n",
      "price: 102.42215549673128\n",
      "price: 102.3845618332135\n",
      "price: 102.32820743209237\n",
      "price: 102.31607378821766\n",
      "price: 102.31370343951504\n",
      "price: 102.329874098877\n",
      "price: 102.34607677328015\n",
      "price: 102.34204465593866\n",
      "price: 102.34398556934846\n",
      "price: 102.34345173886202\n",
      "price: 102.41621657584865\n",
      "price: 102.46125292513067\n",
      "2021-02-25 16:43:49.996030\t回放进度：======= [60%]\n",
      "price: 103.38733652640356\n",
      "price: 103.35975216288837\n",
      "price: 103.30131444560631\n",
      "price: 103.17490481434501\n",
      "price: 103.18360785382352\n",
      "price: 103.17912051245223\n",
      "2021-02-25 16:43:50.045863\t回放进度：======== [70%]\n",
      "price: 102.49553603415939\n",
      "price: 102.52541434453924\n",
      "price: 102.5304016014599\n",
      "price: 102.50699392389028\n",
      "price: 102.45893946457284\n",
      "price: 102.33980154732157\n",
      "price: 102.32209233481412\n",
      "price: 102.31031954239714\n",
      "price: 102.28870818299887\n",
      "price: 102.21590621943368\n",
      "price: 102.15770269739168\n",
      "price: 102.18510970960986\n",
      "price: 102.19671937969476\n",
      "price: 102.18801721410576\n",
      "price: 102.18859599768949\n",
      "price: 102.1889583928157\n",
      "price: 102.17876816214344\n",
      "price: 102.17452090310495\n",
      "price: 102.13213057795478\n",
      "price: 102.14551035916288\n",
      "price: 102.1538783089032\n",
      "price: 102.1631361893415\n",
      "price: 102.16898055185982\n",
      "price: 102.1713708987669\n",
      "price: 102.1672859373272\n",
      "price: 102.13247885276904\n",
      "price: 102.02591128413103\n",
      "price: 102.03288738452575\n",
      "price: 102.02210950992496\n",
      "price: 102.02927371305812\n",
      "price: 102.06733916442707\n",
      "price: 102.0895388545491\n",
      "price: 101.9916245331162\n",
      "price: 101.97894893144695\n",
      "price: 101.96358521694098\n",
      "price: 101.94429055570176\n",
      "price: 101.94947437864407\n",
      "price: 101.9947270994775\n",
      "price: 102.0398624917653\n",
      "price: 102.06145548603713\n",
      "price: 101.79873333405723\n",
      "price: 101.82147378095617\n",
      "price: 101.85585495253947\n",
      "price: 101.88533333333268\n",
      "price: 101.9199740851089\n",
      "price: 101.94976213458406\n",
      "price: 101.97239789077413\n",
      "2021-02-25 16:43:50.100716\t回放进度：========= [80%]\n",
      "price: 101.96208758965633\n",
      "price: 101.97562545400065\n",
      "price: 101.98600970038115\n",
      "price: 101.96063091120952\n",
      "price: 101.8837638233096\n",
      "price: 101.84084770926474\n",
      "price: 101.82757609133253\n",
      "price: 101.82451036439318\n",
      "price: 101.829380587003\n",
      "price: 102.65838327747157\n",
      "price: 102.62267303620885\n",
      "price: 102.5811298885737\n",
      "price: 102.21237330137735\n",
      "price: 101.91662312622113\n",
      "price: 101.95006317989275\n",
      "price: 102.0100817126753\n",
      "price: 102.03003561406955\n",
      "price: 102.03177629689726\n",
      "price: 102.03151234162273\n",
      "price: 102.0482836328882\n",
      "price: 102.0539675422932\n",
      "price: 101.98682015985068\n",
      "price: 101.96670752691983\n",
      "2021-02-25 16:43:50.154608\t回放进度：========== [90%]\n",
      "2021-02-25 16:43:50.154608\t历史数据回放结束\n",
      "2021-02-25 16:43:50.154608\t开始计算逐日盯市盈亏\n",
      "2021-02-25 16:43:50.158597\t逐日盯市盈亏计算完成\n",
      "2021-02-25 16:43:50.158597\t开始计算策略统计指标\n",
      "2021-02-25 16:43:50.174554\t------------------------------\n",
      "2021-02-25 16:43:50.174554\t首个交易日：\t2020-02-05\n",
      "2021-02-25 16:43:50.174554\t最后交易日：\t2020-05-29\n",
      "2021-02-25 16:43:50.174554\t总交易日：\t79\n",
      "2021-02-25 16:43:50.174554\t盈利交易日：\t19\n",
      "2021-02-25 16:43:50.174554\t亏损交易日：\t49\n",
      "2021-02-25 16:43:50.174554\t起始资金：\t1,000,000.00\n",
      "2021-02-25 16:43:50.174554\t结束资金：\t996,995.50\n",
      "2021-02-25 16:43:50.174554\t总收益率：\t-0.30%\n",
      "2021-02-25 16:43:50.174554\t年化收益：\t-0.91%\n",
      "2021-02-25 16:43:50.174554\t最大回撤: \t-2,810.69\n",
      "2021-02-25 16:43:50.174554\t百分比最大回撤: -0.28%\n",
      "2021-02-25 16:43:50.174554\t最长回撤天数: \t113\n",
      "2021-02-25 16:43:50.174554\t总盈亏：\t-3,004.50\n",
      "2021-02-25 16:43:50.174554\t总手续费：\t42.00\n",
      "2021-02-25 16:43:50.174554\t总滑点：\t2,760.00\n",
      "2021-02-25 16:43:50.174554\t总成交金额：\t1,399,957.50\n",
      "2021-02-25 16:43:50.174554\t总成交笔数：\t46\n",
      "2021-02-25 16:43:50.174554\t日均盈亏：\t-38.03\n",
      "2021-02-25 16:43:50.174554\t日均手续费：\t0.53\n",
      "2021-02-25 16:43:50.174554\t日均滑点：\t34.94\n",
      "2021-02-25 16:43:50.174554\t日均成交金额：\t17,720.98\n",
      "2021-02-25 16:43:50.174554\t日均成交笔数：\t0.5822784810126582\n",
      "2021-02-25 16:43:50.174554\t日均收益率：\t-0.00%\n",
      "2021-02-25 16:43:50.174554\t收益标准差：\t0.01%\n",
      "2021-02-25 16:43:50.174554\tSharpe Ratio：\t-5.66\n",
      "2021-02-25 16:43:50.174554\t收益回撤比：\t-1.07\n",
      "2021-02-25 16:43:50.175552\t策略统计指标计算完成\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "@Time    : 2021/2/4 17:15\n",
    "@Author  : jy\n",
    "@FileName: backtesting_demo.py\n",
    "@Software: PyCharm\n",
    "\"\"\"\n",
    "# %%\n",
    "from vnsumscope.app.cta_strategy import (\n",
    "    CtaTemplate,\n",
    "    StopOrder,\n",
    "    TickData,\n",
    "    BarData,\n",
    "    TradeData,\n",
    "    OrderData,\n",
    "    BarGenerator,\n",
    "    ArrayManager,\n",
    ")\n",
    "\n",
    "\n",
    "class TEST01(CtaTemplate):\n",
    "    \"\"\"\"\"\"\n",
    "    author = \"用Python的交易员\"\n",
    "    boll_window = 18\n",
    "    boll_dev = 3.4\n",
    "    cci_window = 10\n",
    "    atr_window = 30\n",
    "    sl_multiplier = 5.2\n",
    "    fixed_size = 1\n",
    "\n",
    "    boll_up = 0\n",
    "    boll_down = 0\n",
    "    cci_value = 0\n",
    "    atr_value = 0\n",
    "\n",
    "    intra_trade_high = 0\n",
    "    intra_trade_low = 0\n",
    "    long_stop = 0\n",
    "    short_stop = 0\n",
    "\n",
    "    parameters = [\n",
    "        \"boll_window\",\n",
    "        \"boll_dev\",\n",
    "        \"cci_window\",\n",
    "        \"atr_window\",\n",
    "        \"sl_multiplier\",\n",
    "        \"fixed_size\"\n",
    "    ]\n",
    "\n",
    "    variables = [\n",
    "        \"boll_up\",\n",
    "        \"boll_down\",\n",
    "        \"cci_value\",\n",
    "        \"atr_value\",\n",
    "        \"intra_trade_high\",\n",
    "        \"intra_trade_low\",\n",
    "        \"long_stop\",\n",
    "        \"short_stop\"\n",
    "    ]\n",
    "\n",
    "    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):\n",
    "        \"\"\"\"\"\"\n",
    "        super().__init__(cta_engine, strategy_name, vt_symbol, setting)\n",
    "\n",
    "        self.bg = BarGenerator(self.on_bar, 15, self.on_15min_bar)\n",
    "        self.am = ArrayManager()\n",
    "\n",
    "    def on_init(self):\n",
    "        \"\"\"\n",
    "        Callback when strategy is inited.\n",
    "        \"\"\"\n",
    "        self.write_log(\"策略初始化\")\n",
    "        self.load_bar(10)\n",
    "\n",
    "    def on_start(self):\n",
    "        \"\"\"\n",
    "        Callback when strategy is started.\n",
    "        \"\"\"\n",
    "        self.write_log(\"策略启动\")\n",
    "\n",
    "    def on_stop(self):\n",
    "        \"\"\"\n",
    "        Callback when strategy is stopped.\n",
    "        \"\"\"\n",
    "        self.write_log(\"策略停止\")\n",
    "\n",
    "    def on_tick(self, tick: TickData):\n",
    "        \"\"\"\n",
    "        Callback of new tick data update.\n",
    "        \"\"\"\n",
    "        self.bg.update_tick(tick)\n",
    "\n",
    "    def on_bar(self, bar: BarData):\n",
    "        \"\"\"\n",
    "        Callback of new bar data update.\n",
    "        \"\"\"\n",
    "        self.write_log(\"收到Bar行情\")\n",
    "        self.bg.update_bar(bar)\n",
    "\n",
    "    def on_15min_bar(self, bar: BarData):\n",
    "        \"\"\"\"\"\"\n",
    "        self.cancel_all()\n",
    "\n",
    "        am = self.am\n",
    "        am.update_bar(bar)\n",
    "        if not am.inited:\n",
    "            return\n",
    "\n",
    "        self.boll_up, self.boll_down = am.boll(self.boll_window, self.boll_dev)\n",
    "        self.cci_value = am.cci(self.cci_window)\n",
    "        self.atr_value = am.atr(self.atr_window)\n",
    "\n",
    "        if self.pos == 0:\n",
    "            self.intra_trade_high = bar.high_price\n",
    "            self.intra_trade_low = bar.low_price\n",
    "\n",
    "            if self.cci_value > 0:\n",
    "                self.write_log(\"开始买开仓报单\")\n",
    "                print('price:', self.boll_up)\n",
    "                self.buy(self.boll_up, self.fixed_size, True)\n",
    "            elif self.cci_value < 0:\n",
    "                self.write_log(\"开始卖开仓报单\")\n",
    "                self.short(self.boll_down, self.fixed_size, True)\n",
    "\n",
    "        elif self.pos > 0:\n",
    "            self.intra_trade_high = max(self.intra_trade_high, bar.high_price)\n",
    "            self.intra_trade_low = bar.low_price\n",
    "\n",
    "            self.long_stop = self.intra_trade_high - self.atr_value * self.sl_multiplier\n",
    "            self.write_log(\"开始卖平仓报单\")\n",
    "            self.sell(self.long_stop, abs(self.pos), True)\n",
    "\n",
    "        elif self.pos < 0:\n",
    "            self.intra_trade_high = bar.high_price\n",
    "            self.intra_trade_low = min(self.intra_trade_low, bar.low_price)\n",
    "\n",
    "            self.short_stop = self.intra_trade_low + self.atr_value * self.sl_multiplier\n",
    "            self.write_log(\"开始买平仓报单\")\n",
    "            self.cover(self.short_stop, abs(self.pos), True)\n",
    "\n",
    "        self.put_event()\n",
    "\n",
    "    def on_order(self, order: OrderData):\n",
    "        \"\"\"\n",
    "        Callback of new order data update.\n",
    "        \"\"\"\n",
    "        pass\n",
    "\n",
    "    def on_trade(self, trade: TradeData):\n",
    "        \"\"\"\n",
    "        Callback of new trade data update.\n",
    "        \"\"\"\n",
    "        self.put_event()\n",
    "\n",
    "    def on_stop_order(self, stop_order: StopOrder):\n",
    "        \"\"\"\n",
    "        Callback of stop order update.\n",
    "        \"\"\"\n",
    "        pass\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    from vnsumscope.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting\n",
    "    from datetime import datetime\n",
    "\n",
    "    engine = BacktestingEngine()\n",
    "    engine.set_parameters(\n",
    "        vt_symbol=\"T2006.CFFEX\",\n",
    "        interval=\"1m\",\n",
    "        start=datetime(2020, 1, 15),\n",
    "        end=datetime(2020, 5, 30),\n",
    "        rate=0.3 / 10000,\n",
    "        slippage=0.2,\n",
    "        size=300,\n",
    "        pricetick=0.2,\n",
    "        capital=1_000_000,\n",
    "    )\n",
    "    engine.add_strategy(TEST01, {})\n",
    "    engine.load_data()\n",
    "    engine.run_backtesting()\n",
    "    df = engine.calculate_result()\n",
    "    engine.calculate_statistics()\n",
    "    engine.show_chart()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
